import Vue from 'vue'
import VueRouter from 'vue-router'
import Home from '../views/Home.vue'
import UserCenter from '../views/UserCenter.vue'

import Index from '../views/Index.vue'
import User from '../views/sys/User.vue'
import Role from '../views/sys/Role.vue'
import Menu from '../views/sys/Menu.vue'

import axios from '../axios'
import store from "../store"
Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    name: 'Home',
    component: Home,
    children: [
      {
        path: '/index',
        name: 'Index',
        meta: {
          title:'首页'
        },
        component: Index
      },
      // {
      //   path: '/sys/users',
      //   name: 'User',
      //   component: User
      // },
      // {
      //   path: '/sys/roles',
      //   name: 'Role',
      //   component: Role
      // },
      // {
      //   path: '/sys/menus',
      //   name: 'Menu',
      //   component: Menu
      // },
      {
        path: '/userCenter',
        name: 'UserCenter',
        meta: {
          title:'个人中心'
        },
        component: UserCenter
      }
    ]
  },
  // {
  //   path: '/index',
  //   name: 'Index',
  //   component: Index
  // },
  {
    path: '/login',
    name: 'Login',
    component: () => import('../views/Login.vue')//这里引用了最上面就不用引用了
  }
]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})

router.beforeEach((to, from, next) => {
  let hasRoute = store.state.menu.hasRoute
  let token = localStorage.getItem("token")
  // console.log("token:")
  // console.log(token)
  if (to.path == '/login') {
    next()
  }
  else if (!token) {
    next({ path: '/login' })
  }
  else if (!hasRoute && token) {
    axios.get("/sys/menu/nav", {
      headers: {
        Authorization: localStorage.getItem("token")
      }
    }).then(res => {

      // console.log(res.data.data)

      // 拿到menuList
      store.commit("setMenuList", res.data.data.nav)

      // 拿到用户权限
      store.commit("setPermList", res.data.data.authoritys)

      // console.log(store.state.menu.menuList)
      //动态绑定路由
      let newRoutes = router.options.routes
      res.data.data.nav.forEach((menu => {
        if (menu.children) {
          menu.children.forEach(e => {
            //转换路由
            let route = menuToRoute(e)

            //把路由添加到路由管理
            if (route) {
              newRoutes[0].children.push(route)
            }

          })
        }
      }))
      // console.log(newRoutes)
      router.addRoutes(newRoutes)
      hasRoute = true
      store.commit('changeRouteStatus', hasRoute)
    })
  }
  next()
})
//转成路由
const menuToRoute = (menu) => {
  if (!menu.component) {
    return null
  }
  let route = {
    name: menu.name,
    path: menu.path,
    meta: {
      icon: menu.icon,
      title: menu.title
    }
  }
  route.component = () => import('@/views/' + menu.component + '.vue')
  return route
}


export default router
